package com.cuckoo.mall.thus.menuaccount.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bgn.baseframe.base.BaseActivity;
import com.bgn.baseframe.network.HttpObserver;
import com.bgn.baseframe.network.bean.TDataBean;
import com.bgn.baseframe.utils.ToastUtil;
import com.cuckoo.mall.R;
import com.cuckoo.mall.thus.manager.UserInfoManager;
import com.cuckoo.mall.network.ApiManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


@Route(path = "/mall/settingNameActivity", group = "mall")
public class SettingNameActivity extends BaseActivity {


    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_comfirm)
    TextView tvComfirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_name);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        getTitleBar().setTitle(R.string.user_name);
        etName.setText(UserInfoManager.getInstance().getUserInfo().getData().getNick_name());
        etName.setSelection(etName.getText().toString().length());
        if (TextUtils.isEmpty(etName.getText().toString())) {
            tvComfirm.setEnabled(false);
        }
        etName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(etName.getText().toString())) {
                    tvComfirm.setEnabled(true);
                } else {
                    tvComfirm.setEnabled(false);
                }
            }
        });
    }


    @OnClick(R.id.tv_comfirm)
    public void onViewClicked() {
        String name = etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtil.showInfo(getString(R.string.please_input_nick_name));
        } else if (name.length() > 20) {
            ToastUtil.showInfo(getString(R.string.nick_name_rule));
        } else {
            showLoadingView();
            ApiManager.modifyUserInfo(etName.getText().toString()).subscribe(new HttpObserver<TDataBean>(this) {

                @Override
                public void onSucceed(TDataBean value) {
                    ToastUtil.showInfo(getString(R.string.update_succeed));
                    UserInfoManager.getInstance().getUserInfo().getData().setNick_name(etName.getText().toString().trim());
                    setResult(RESULT_OK);
                    finish();
                }
            });
        }
    }
}
